#pragma once
#include "EventLoop.h"
#include "Buffer.h"
#include "Channel.h"
#include "BaseTask.h"
#include <stdlib.h>
#include <stdio.h>
#include "Log.h"
#include "WorkerThreadPool.h"
#include "block_log.h"

// 添加定时器状态枚举
enum class TimerState {
    TIMER_ACTIVE,    // 定时器活跃
    TIMER_EXPIRED,   // 定时器已过期
    TIMER_CANCELLED  // 定时器已取消
};


class TcpConnection
{
public:
    TcpConnection(int fd, EventLoop *evloop);
    ~TcpConnection();

    static int processRead(void *arg);
    static int processWrite(void *arg);
    static int destroy(void *arg);
    
    // 异步发送响应数据（线程安全）
    void sendResponseAsync(const std::vector<uint8_t>& data);
    
    EventLoop *m_evLoop;
    Channel *m_channel;

    Buffer *m_readBuf;
    Buffer *m_writeBuf;

    // 定时器相关成员
    std::chrono::steady_clock::time_point expire_time;
    size_t heap_index; // 在堆中的下标，便于调整
    TimerState timer_state; // 定时器状态

    // 设置/获取定时器状态
    void setTimerState(TimerState state) { timer_state = state; }
    TimerState getTimerState() const { return timer_state; }
    
    // 用户登录状态管理（用于长连接，不需要 token）
    void setUserId(uint64_t userId) { m_userId = userId; m_isLoggedIn = true; }
    uint64_t getUserId() const { return m_userId; }
    bool isLoggedIn() const { return m_isLoggedIn; }
    void logout();  // 用户登出（更新数据库状态）

private:
    string m_name;
    
    // 用户登录信息
    uint64_t m_userId = 0;        // 登录用户的 ID
    bool m_isLoggedIn = false;    // 是否已登录
};
